package com.ruoyi.eam.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.doman.EamType;
import com.ruoyi.eam.mapper.*;
import com.ruoyi.eam.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.doman.EamMaintain;
import com.ruoyi.eam.service.IEamMaintainService;

/**
 * 资产维修Service业务层处理
 *
 * @author xl
 * @date 2024-07-16
 */
@Service
public class EamMaintainServiceImpl implements IEamMaintainService
{
    @Autowired
    private EamMaintainMapper eamMaintainMapper;
    @Autowired
    private EamAssetMapper assetMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EamTypeMapper typeMapper;
    @Autowired
    private EamLocationMapper eamLocationMapper;
    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 查询资产维修
     *
     * @param maintainId 资产维修主键
     * @return 资产维修
     */
    @Override
    public EamMaintain selectEamMaintainByMaintainId(Long maintainId)
    {
        return eamMaintainMapper.selectEamMaintainByMaintainId(maintainId);
    }

    /**
     * 查询资产维修列表
     *
     * @param eamMaintain 资产维修
     * @return 资产维修
     */
    @Override
    public List<EamMaintain> selectEamMaintainList(EamMaintain eamMaintain)
    {
        List<EamMaintain> list = eamMaintainMapper.selectEamMaintainList(eamMaintain);
        for (EamMaintain maintain : list) {
            System.out.println(maintain.getAssetId());
            EamAsset asset = assetMapper.selectEamAssetByAssetId(maintain.getAssetId());
            System.out.println("123:"+asset);
            asset.setTypeName(typeMapper.selectEamTypeByTypeId(asset.getTypeId()).getTypeName());
            if(asset.getLocationId() != null){
                asset.setLocationName(eamLocationMapper.selectEamLocationByLocationId(asset.getLocationId()).getLocationName());
            }
            asset.setStatusName(dictDataMapper.selectDictDataByDictCode(asset.getUseStatus()).getDictLabel());
            maintain.setAsset(asset);
            maintain.setCreatorName(userMapper.selectUserByUserId(maintain.getCreator()).getNickName());
        }

        return list;
    }

    /**
     * 新增资产维修
     *
     * @param eamMaintain 资产维修
     * @return 结果
     */
    @Override
    public int insertEamMaintain(EamMaintain eamMaintain)
    {
        eamMaintain.setCreateTime(DateUtils.getNowDate());
        //业务处理
        //获取当前时间
        String nowDate = DateUtils.getDateYYYYMMDD();
        //获取最后一条借用编码
        String chgCodeLast = eamMaintainMapper.queryEamMaintainList().get(eamMaintainMapper.queryEamMaintainList().size() - 1).getMaintainCode();
        //截取流水线
        String serial =  chgCodeLast.substring(10,16);
        if(serial.equals("999999")){
            serial = "000001";
        }else{
            int num = Integer.parseInt(serial) + 1;
            serial = String.format("%06d",num);
        }
        //最终编码
        String chgCode = EamConstants.WX_CODE_PR + nowDate + serial;
        eamMaintain.setMaintainCode(chgCode);
        eamMaintain.setIsdel(1L);
        return eamMaintainMapper.insertEamMaintain(eamMaintain);
    }

    /**
     * 修改资产维修
     *
     * @param eamMaintain 资产维修
     * @return 结果
     */
    @Override
    public int updateEamMaintain(EamMaintain eamMaintain)
    {
        return eamMaintainMapper.updateEamMaintain(eamMaintain);
    }

    /**
     * 批量删除资产维修
     *
     * @param maintainIds 需要删除的资产维修主键
     * @return 结果
     */
    @Override
    public int deleteEamMaintainByMaintainIds(Long[] maintainIds)
    {
        return eamMaintainMapper.deleteEamMaintainByMaintainIds(maintainIds);
    }

    /**
     * 删除资产维修信息
     *
     * @param maintainId 资产维修主键
     * @return 结果
     */
    @Override
    public int deleteEamMaintainByMaintainId(Long maintainId)
    {
        return eamMaintainMapper.deleteEamMaintainByMaintainId(maintainId);
    }

    @Override
    public List<EamAsset> queryAssetMaintain(EamAsset eamAsset) {
        List<EamAsset> assets  = assetMapper.queryAssetMaintain(eamAsset);
        for (EamAsset asset : assets) {
            asset.setTypeName(typeMapper.selectEamTypeByTypeId(asset.getTypeId()).getTypeName());
            if(asset.getLocationId() != null){
                asset.setLocationName(eamLocationMapper.selectEamLocationByLocationId(asset.getLocationId()).getLocationName());
            }
            asset.setStatusName(dictDataMapper.selectDictDataByDictCode(asset.getUseStatus()).getDictLabel());
        }
        return assets;
    }
}
